<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="135px" @keyup.enter="submitHandle()">
      <el-row>
        <el-col :span="12">
          <el-form-item label="" prop="id">
            <el-input v-model="dataForm.id" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="" prop="imei">
            <el-input v-model="dataForm.imei" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="type">
            <el-radio-group v-model="dataForm.type">
              <el-radio :value="1">物联卡续费</el-radio>
              <el-radio :value="2">平台续费</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="sim卡到期时间" prop="simEndDate">
            <el-input v-model="dataForm.simEndDate" placeholder="sim卡到期时间"></el-input>
          </el-form-item>
          <el-form-item label="平台到期时间" prop="platformEndDate">
            <el-input v-model="dataForm.platformEndDate" placeholder="平台到期时间"></el-input>
          </el-form-item>
          <el-form-item label="sim卡到期时间" prop="newSimEndDate">
            <el-input v-model="dataForm.newSimEndDate" placeholder="新的 sim卡到期时间"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间" prop="crtTime">
            <el-input v-model="dataForm.crtTime" placeholder="创建时间"></el-input>
          </el-form-item>
          <el-form-item label="更新时间" prop="upTime">
            <el-input v-model="dataForm.upTime" placeholder="更新时间"></el-input>
          </el-form-item>
          <el-form-item label="sim 卡号" prop="sim">
            <el-input v-model="dataForm.sim" placeholder="sim 卡号"></el-input>
          </el-form-item>
          <el-form-item label="续费订单号" prop="sn">
            <el-input v-model="dataForm.sn" placeholder="续费订单号"></el-input>
          </el-form-item>
          <el-form-item label="新的平台到期时间" prop="newPlatformEndDate">
            <el-input v-model="dataForm.newPlatformEndDate" placeholder="新的 平台到期时间"></el-input>
          </el-form-item>
          <el-form-item label="订单编号" prop="orderId">
            <el-input v-model="dataForm.orderId" placeholder="订单编号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useDeviceRenewRecorderApi, useDeviceRenewRecorderSubmitApi } from '@/api/gps/device_renew_recorder'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
  id: '',
  imei: '',
  type: '',
  simEndDate: '',
  platformEndDate: '',
  newSimEndDate: '',
  newPlatformEndDate: '',
  orderId: '',
  crtTime: '',
  upTime: '',
  sim: '',
  sn: ''
})

const init = (id?: number) => {
  visible.value = true
  dataForm.id = ''

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  if (id) {
    getDeviceRenewRecorder(id)
  }
}

const getDeviceRenewRecorder = (id: number) => {
  useDeviceRenewRecorderApi(id).then((res) => {
    Object.assign(dataForm, res.data)
  })
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    useDeviceRenewRecorderSubmitApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
